/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   SystemBase.cs
 * CreateData   :   2023/4/28 16:53:49
 * UnityVersion :   2020.3.33f1c2
 * Description  :   系统基类
************************************************/

using Main;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Hotfix
{
    /// <summary>
    /// 系统即为行为
    /// 系统基类
    /// </summary>
    internal class SystemBase : ISystemComponent
    {
        /// <summary>
        /// 用于驱动生命周期回调
        /// </summary>
        public EventEmitter<SystemCallbackType> SystemCallback;
        /// <summary>
        /// 系统所在的实体
        /// </summary>
        public EntityBase Entity { get; private set; }
        private bool active = true;
        /// <summary>
        /// 启用禁用系统
        /// </summary>
        public bool Enable
        {
            get
            {
                return active;
            }
            set
            {
                if (active != value)
                {
                    active = value;
                    if (Entity.Enable && Entity.Scene.isLoaded)
                    {
                        if (active)
                        {
                            SystemCallback.Emit(SystemCallbackType.Awake);
                            SystemCallback.Emit(SystemCallbackType.OnEnable);
                            SystemCallback.Emit(SystemCallbackType.Start);
                        }
                        else
                        {
                            SystemCallback.Emit(SystemCallbackType.OnDisable);
                        }
                    }
                }
            }
        }
        public SystemBase()
        {
            SystemCallback = new EventEmitter<SystemCallbackType>();
            SystemCallback.Once(SystemCallbackType.Awake, Awake);
            SystemCallback.Once(SystemCallbackType.OnEnable, OnEnableCallback);
            SystemCallback.Once(SystemCallbackType.Start, Start);
            SystemCallback.On(SystemCallbackType.Update, Update);
            SystemCallback.On(SystemCallbackType.LateUpdate, LateUpdate);
            SystemCallback.On(SystemCallbackType.FixedUpdate, FixedUpdate);
            SystemCallback.Once(SystemCallbackType.OnDestroy, OnDestroy);
        }
        private void OnEnableCallback()
        {
            OnEnable();
            SystemCallback.Once(SystemCallbackType.OnDisable, OnDisableCallback);
        }
        private void OnDisableCallback()
        {
            OnDisable();
            SystemCallback.Once(SystemCallbackType.OnEnable, OnEnableCallback);
        }

        #region 生命周期回调
        /// <summary>
        /// 添加系统时调用
        /// </summary>
        public virtual void Awake()
        {

        }
        /// <summary>
        /// Awake 之后执行（请注意：对于不同Entity而言Start并不会一定在Awake之后执行）
        /// </summary>
        public virtual void Start()
        {

        }
        /// <summary>
        /// 系统启用执行
        /// </summary>
        public virtual void OnEnable()
        {

        }
        /// <summary>
        /// 系统禁用执行
        /// </summary>
        public virtual void OnDisable()
        {

        }
        /// <summary>
        /// 每帧执行
        /// </summary>
        public virtual void Update()
        {

        }
        /// <summary>
        /// Update之后执行
        /// </summary>
        public virtual void LateUpdate()
        {

        }
        /// <summary>
        /// 固定物理帧执行
        /// </summary>
        public virtual void FixedUpdate()
        {

        }
        /// <summary>
        /// 系统销毁执行
        /// </summary>
        public virtual void OnDestroy()
        {

        }
        #endregion
    }
}
